package ru.spb.osll.myfit.server.execute;

import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;

import java.lang.reflect.Constructor;

import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;

import fit.FixtureLoader;
import fit.Parse;
import fitnesse.wiki.WikiPage;

public class FitNesseTestRunnerTest extends EasyMockSupport {

 public class TestableFitNesseTestRunner extends FitNesseTestRunner {
			
				public TestableFitNesseTestRunner(String rootPath,
                                      String rootDirectoryName,
                                      FixtureLoader fixtureLoader,
                                      ProgressFixtureListener listener)
        throws Exception {
				 m_fixtureListener = listener;
				 m_fixtureLoader = fixtureLoader;
				}

        @Override
				public Parse getParse(String pagePath) throws Exception {
          return super.getParse(pagePath);
				}
				
				@Override
				public WikiPage createRoot(String rootPath, String rootDirectoryName) throws Exception{
				  return null;
				}
	}

	private static final String HTML_PAGE = "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td>Import</td></tr>"
			+ "<tr><td>ru.spb.osll.myfit.fixtures.base</td>"
			+ "</tr></table>"
			+ "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td class=\"pass\">dummy one line</td>"
			+ "<td>OK</td></tr></table>";

	private static final String RESULT_HTML = "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td>Import</td></tr>"
			+ "<tr><td>ru.spb.osll.myfit.fixtures.base</td></tr>"
			+ "</table>"
			+ "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td class=\"pass\" class=\"pass\">dummy one line</td><td>OK</td></tr>"
			+ "</table>";

	private static final String ERROR_RESULT_HTML = "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td>Import</td></tr>"
			+ "<tr><td>ru.spb.osll.myfit.fixtures.base</td></tr>"
			+ "</table>"
			+ "<table border=\"1\" cellspacing=\"0\" class=\"error\">"
			+ "<tr><td class=\"pass\">dummy one line</td><td>OUPS</td></tr>"
			+ "</table>";

	private FitNesseTestRunner testRunner;
	private MyFitFixtureListener listener;

	@Before
	public void setUp() throws SecurityException, NoSuchMethodException {
		listener = new MyFitFixtureListener();
		Constructor<FitNesseTestRunnerTest.TestableFitNesseTestRunner> constructor = FitNesseTestRunnerTest.TestableFitNesseTestRunner.class.getConstructor(FitNesseTestRunnerTest.class, String.class, String.class, FixtureLoader.class, ProgressFixtureListener.class);
		testRunner = createMockBuilder(TestableFitNesseTestRunner.class).withConstructor(constructor).withArgs(this, "path/to/fitnesse",
        "FitNesseRoot", FixtureLoader.instance(),listener).addMockedMethod("getParse", String.class).createMock();
	}

	@Test
	public void testExecuteTest() throws Exception {
	  expect(testRunner.getParse("TestSuite.TestPage")).andReturn(new Parse(HTML_PAGE));
	  replayAll();
		testRunner.run("TestSuite.TestPage");
		String result = listener.getOutput().replace("&nbsp;", "");
		assertEquals(RESULT_HTML, result);
    verifyAll();
	}

	@Test
	public void testExecuteTestWithException() throws Exception {
	  expect(testRunner.getParse("TestSuite.TestPage2")).andStubReturn(new Parse(HTML_PAGE.replaceAll("OK", "OUPS")));
	  replayAll();
		testRunner.run("TestSuite.TestPage2");
		String result = listener.getOutput().replace("&nbsp;", "");
		verifyAll();
		assertEquals(ERROR_RESULT_HTML, result);
	}
	
}
